package net.k3rnel.abugadro.gcodes;

import net.k3rnel.abugadro.gcodes.Issue.Issues;
import net.k3rnel.abugadro.gcodes.Label.Labels;
import net.k3rnel.abugadro.providers.ProjectsDB;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.util.Log;


public class IssueFeedHandler extends DefaultHandler{

	private ContentResolver content;
	private ContentValues currentIssue;
	private StringBuilder builder;
	private int projectId;
	private String issueId;
	private boolean inEntry = false;

	// names of the XML tags
	static final String FEED = "feed";
	static final String ENTRY = "entry";
	static final String AUTHOR = "author";
	static final String OWNER = "owner";

	static final String PUB_DATE = "published";
	static final String UP_DATE = "updated";
	static final String TITLE = "title";
	static final String CONTENT = "content";
	static final String AUTHOR_NAME = "name";
	static final String AUTHOR_URI = "uri";
	static final String ISSUES_ID = "id";
	static final String ISSUES_LABEL = "label";
	static final String ISSUES_STARS = "stars";
	static final String ISSUES_STATE = "state";
	static final String ISSUES_STATUS = "status";

	public IssueFeedHandler(ContentResolver content, int projectId) {
		this.content = content;
		this.projectId = projectId;
	}

	@Override
	public void characters(char[] ch, int start, int length)
	throws SAXException {
		super.characters(ch, start, length);
		builder.append(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String name)
	throws SAXException {
		super.endElement(uri, localName, name);
		if (this.currentIssue != null){
			if (localName.equalsIgnoreCase(TITLE)&&inEntry){
				currentIssue.put(Issues.TITLE,builder.toString());
			} else if (localName.equalsIgnoreCase(CONTENT)&&inEntry){
				currentIssue.put(Issues.CONTENT,builder.toString());
			} else if (localName.equalsIgnoreCase(PUB_DATE)&&inEntry){
				currentIssue.put(Issues.PUBLISHED,builder.toString());
			} else if (localName.equalsIgnoreCase(UP_DATE)&&inEntry){
				currentIssue.put(Issues.UPDATED,builder.toString());
			} else if (localName.equalsIgnoreCase(AUTHOR_NAME)&&inEntry){
				currentIssue.put(Issues.AUTHOR,builder.toString());
			} else if (localName.equalsIgnoreCase(OWNER)&&inEntry){
				currentIssue.put(Issues.OWNER,builder.toString());
			} else if (localName.equalsIgnoreCase(ISSUES_ID)&&inEntry){
				issueId = builder.toString();
				currentIssue.put(Issues.ISSUE_ID,builder.toString());
			} else if (localName.equalsIgnoreCase(ISSUES_LABEL)&&inEntry){
				if(!ProjectsDB.getInstance().isLabelInDB(content, projectId, issueId, builder.toString())){
					ContentValues label = new ContentValues();
					label.put(Labels.ISSUE_ID,issueId);
					label.put(Labels.PROJECT_ID,projectId);
					label.put(Labels.LABEL,builder.toString());
					content.insert(Labels.CONTENT_URI, label);
				}
			} else if (localName.equalsIgnoreCase(ISSUES_STARS)&&inEntry){
				currentIssue.put(Issues.STARS,builder.toString());
			} else if (localName.equalsIgnoreCase(ISSUES_STATE)&&inEntry){
				currentIssue.put(Issues.STATE,builder.toString());
			} else if (localName.equalsIgnoreCase(ISSUES_STATUS)&&inEntry){
				currentIssue.put(Issues.STATUS,builder.toString());
			} else if (localName.equalsIgnoreCase(ENTRY)&&inEntry){
				if(!ProjectsDB.getInstance().isIssueInDB(content, projectId, issueId)){
					Log.i("IssueFeedHandler","Created new Issue: "+projectId+"/"+issueId);
					content.insert(Issues.CONTENT_URI, currentIssue);
				}else{
					content.update(Issues.CONTENT_URI,currentIssue,Issues.ISSUE_ID + "='" + issueId + "' AND "+Issues.PROJECT_ID + "= '"+projectId+"'",null);
					Log.i("IssueFeedHandler","Updated Issue: "+projectId+"/"+issueId);
				}
				currentIssue = new ContentValues();
				inEntry=false;
			}
			builder.setLength(0);	
		}
	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		builder = new StringBuilder();
	}

	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, name, attributes);
		if (localName.equalsIgnoreCase(ENTRY)){
			inEntry = true;
			this.currentIssue = new ContentValues();
			currentIssue.put(Issues.PROJECT_ID,projectId);
		}
	}
}